<?php

class AddateAction extends CommonAction {

    // 查询数据
    public function index() {
       
		//$searchstartdate = $this->_param('searchstartdate');
		$searchstartdate = '2013-11-01';
		$searchenddate = '2013-11-30' ;
		 
	 
		if($searchstartdate && $searchenddate){
			$yearyue = array();
			$ri = array();
			$time_start = $thistime = strtotime($searchstartdate);
			$time_end = strtotime($searchenddate);
			while($thistime >= $time_start && $thistime <= $time_end){
				$yearyue[date('Y-m',$thistime)]++;
				$ri[date('Y-m-d',$thistime)][0] = date('j',$thistime);
				$ri[date('Y-m-d',$thistime)][1] = date('w',$thistime);
				$thistime = $thistime + 86400;
			}
			foreach($yearyue as $k=>$v){
				$yearyue_a[] = "{title:'".$k."',colspan:".$v."}";
			}
			foreach($ri as $k=>$v){
				if($v[1] == '0' || $v[1]=='6'){
					$suncolor = "<font color=#0000FF>".$v[0]."</font>";
				}else{
					$suncolor = $v[0];
				}
				$ri_a[] = "{field:'".$k."',title:'".$suncolor."',width:20,align:'center'}";
			}
		}
		//print_r($yearyue_a);
		$this->assign('yearyue_a',$yearyue_a);
		$this->assign('ri_a',$ri_a);
		
        $this->display();
    }
	
	 public function indexjson() {
	 
		$page = max(1,$page);
		$rows =     $this->_param('rows')? $this->_param('rows') : 10;	//echo $rows;
		$limitstart = $rows*($page-1);
		$sqladd = "";
		if($searchchannelid){
			$channelchildren[] = "'".$searchchannelid."'";
			//channelchildren($searchchannelid);
			//$channelchildren = array_unique($channelchildren);
			//$sqladd .= " AND a.channelid IN(".implode(',',$channelchildren).") ";
		}
		$searchstartdate = '2013-11-01';
		$searchenddate = '2013-11-30' ;
		if($searchstartdate && $searchenddate){
			$dayi = array();
			$time_start = $thistime = strtotime($searchstartdate);
			$time_end = strtotime($searchenddate);
			while($thistime >= $time_start && $thistime <= $time_end){
				$dayi[] = date('Y-m-d',$thistime);
				$thistime = $thistime + 86400;
			}
		}
	  
	 
	 
        $Addate = M("Addate");
        //$list = $Addate->limit(3)->order('id desc')->select();
		//$sql_adpace_count = "SELECT count(*) AS num FROM think_adplace AS a LEFT JOIN think_channel AS b ON a.channelid=b.id WHERE a.status!='1' ".$sqladd." ORDER BY a.channelid,a.name";
		
		
		$sql_adpace_list = "SELECT a.*,b.channelname FROM think_adplace AS a LEFT JOIN think_channel AS b ON a.channelid=b.id WHERE a.status!='1' ".$sqladd." ORDER BY a.channelid,a.name LIMIT ".$limitstart.",".$rows;
		//echo $sql_adpace_list;
		$adpace_list = $Addate->query($sql_adpace_list);
		$i = 0;
		foreach($adpace_list AS $adpace ){
				$sql_adpace_date = "SELECT a.date,b.status FROM think_date AS a LEFT JOIN think_order_info AS b ON a.orderid=b.id WHERE a.channelid='".$adpace['channelid']."' AND a.adplaceid='".$adpace['id']."' AND a.date>='".$searchstartdate."' AND a.date<='".$searchenddate."'";
				//$sql_adpace_date = "SELECT a.date,b.title,b.status FROM think_date AS a LEFT JOIN think_order_info AS b ON a.orderid=b.id WHERE a.channelid='4' AND a.adplaceid='1' AND a.date>='2013-11-01' AND a.date<='2013-11-30'";
				
				
				$adpace_date_list = $Addate->query($sql_adpace_date);
				//echo $Addate->getLastSql();
				//dump($list);exit;
				
				foreach($adpace_date_list AS $row_date ){
					$orderdate[$row_date['date']]['username'] = $row_date['username'];
					$orderdate[$row_date['date']]['title'] = $row_date['title'];
					$orderdate[$row_date['date']]['code'] = 'XA-'.$row_date['code'];
					$orderdate[$row_date['date']]['status'] = $row_date['status'];
				}
				//print_r($orderdate);
				foreach($dayi as $k=>$v){
					//if($orderdate[$v]['code']){
						if($orderdate[$v]['status']=='1'){
							$status = '已审核';
						}else{
							$status = '新申请';
						}
						$adplace[$i][$v] = '<font color=red title="申请人：'.$orderdate[$v]['username']."\n订单标题：".$orderdate[$v]['title']."\n订单编号：".$orderdate[$v]['code']."\n状态：".$status.'">1</font>';
					//}
				}
				
				
		$adplace[$i]['table_channel'] = $adpace['channelname'];
		$adplace[$i]['table_adplace'] = $adpace['name'];
		$adplace[$i]['table_adplace_w_h']['w'] = $adpace['width'];
		$adplace[$i]['table_adplace_w_h']['h'] = $adpace['height'];
		$adplace[$i]['table_status'] = $adpace['status'];
		$i++;
		}
		//print_r($adplace);		
			
		$addateHtml['total'] = $row_sum['num'];
		$addateHtml['rows'] = $adplace;
		echo json_encode($addateHtml);
		
    }

    // 写入数据
    public function insert() {
        $Addate = D("Addate");
        if ($vo = $Addate->create()) {
            $list = $Addate->add();
            if ($list !== false) {
                $this->success('数据保存成功！',U('Index/index'));
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($Addate->getError());
        }
    }

    // 更新数据
    public function update() {
        $Addate = D("Addate");
        if ($vo = $Addate->create()) {
            $list = $Addate->save();
            if ($list !== false) {
                $this->success('数据更新成功！',U('Index/index'));
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($Addate->getError());
        }
    }

    // 删除数据
    public function delete($id) {
        if (!empty($id)) {
            $Addate = M("Addate");
            $result = $Addate->delete($id);
            if (false !== $result) {
                $this->success('删除成功！');
            } else {
                $this->error('删除出错！');
            }
        } else {
            $this->error('ID错误！');
        }
    }

    // 编辑数据
    public function edit($id) {
        if (!empty($id)) {
            $Addate = M("Addate");
            $vo = $Addate->getById($id);
            if ($vo) {
                $this->vo   =   $vo;
                $this->display();
            } else {
                $this->error('数据不存在！');
            }
        } else {
            $this->error('数据不存在！');
        }
    }
}
?>